package com.ilindo.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5
{
	/**
	 * 对目标字符串进行MD5加密
	 * 
	 * @param source
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws NoSuchAlgorithmException
	 * @throws Exception
	 */
	public static String md5(String source) throws UnsupportedEncodingException, NoSuchAlgorithmException
	{
		String resultHash = null;
		try
		{
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] result = new byte[md5.getDigestLength()];
			md5.reset();
			md5.update(source.getBytes("UTF-8"));
			result = md5.digest();

			StringBuffer buf = new StringBuffer(result.length * 2);

			for (int i = 0; i < result.length; i++)
			{
				int intVal = result[i] & 0xff;
				if (intVal < 0x10)
				{
					buf.append("0");
				}
				buf.append(Integer.toHexString(intVal));
			}
			resultHash = buf.toString();
		}
		catch (Exception e)
		{

		}
		return resultHash;
	}

}
